package com.cloudm.technician.ui.base

import android.support.v4.content.ContextCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewStub
import com.cloudm.technician.R
import com.cloudm.technician.data.model.LoadMoreInfo
import com.cloudm.technician.extensions.toast
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
import kotlinx.android.synthetic.main.common_fragment_list.*
import kotlinx.android.synthetic.main.common_recycler_view.*

/**
 * 作者: wesley
 * 时间: 2017/11/21
 * 描述: 列表fragment
 */
abstract class BaseListFragment<T : BasePresenter, K> : BasePermissionsFragment<T>(),
        OnRefreshListener, OnLoadmoreListener, BaseListView<ArrayList<K>>,
        BaseAdapter.OnItemClickListener<K> {

    private lateinit var viewStub: ViewStub
    protected var startPage = 1
    private var status = 0 //刷新状态 1：加载更多

    companion object {
        const val PAGE_SIZE = 10
    }

    protected open val enabledRefresh: Boolean by lazy {
        false
    }

    protected open val enabledLoadMore: Boolean by lazy {
        true
    }

    protected open val dividerEnabled: Boolean by lazy {
        true
    }

    protected open val progressEnabled: Boolean by lazy {
        true
    }

    override val resource: Int by lazy {
        R.layout.common_fragment_list
    }

    protected open val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(mContext)
    }

    protected open val itemDecoration: RecyclerView.ItemDecoration by lazy {
        DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL).apply {
            val drawable = ContextCompat.getDrawable(mContext, R.drawable.drawable_common_divider)
            drawable?.let {
                setDrawable(it)
            }
        }
    }

    private val adapter: BaseAdapter<K> by lazy {
        createAdapter()
    }

    override fun initVariables() {
        adapter.onItemClickListener = this
    }

    override fun initViews(view: View) {
        viewStub = view.findViewById(R.id.viewStub)
        when (progressEnabled) {
            true -> progressBar.visibility = View.VISIBLE
            else -> progressBar.visibility = View.GONE
        }
        initEmptyResource()
        initHeaderAndFooter()
        initRecyclerView()
        loadData()
    }

    //设置header和footer
    private fun initHeaderAndFooter() {
        smartRefreshLayout.isEnableRefresh = enabledRefresh
        smartRefreshLayout.isEnableLoadmore = enabledLoadMore
        smartRefreshLayout.refreshHeader = ClassicsHeader(mContext)
        smartRefreshLayout.refreshFooter = ClassicsFooter(mContext)
    }


    //初始化recyclerView
    private fun initRecyclerView() {
        if (recyclerView is SwipeMenuRecyclerView) {
            initSwipeMenu()
        }
        recyclerView.layoutManager = layoutManager
        if (dividerEnabled) {
            recyclerView.addItemDecoration(itemDecoration)
        }
        recyclerView.adapter = adapter
    }

    override fun initListener() {
        smartRefreshLayout.setOnRefreshListener(this)
        smartRefreshLayout.setOnLoadmoreListener(this)
    }


    override fun onRefresh(refreshLayout: RefreshLayout?) {
        status = 0
        startPage = 1
        loadData()
    }


    override fun onLoadmore(refreshLayout: RefreshLayout?) {
        status = 1
        startPage++
        loadData()
    }


    override fun loadSuccess(t: ArrayList<K>?, loadMore: LoadMoreInfo?) {
        when (status) {
            0 -> {
                smartRefreshLayout.finishRefresh()
                if (t == null || t.size == 0) {
                    showEmptyView()
                } else {
                    showContentView()
                    adapter.clear()
                    adapter.addAllItems(t)
                    smartRefreshLayout.isLoadmoreFinished = loadMore == null || loadMore.last
                }
            }

            1 -> {
                showContentView()
                smartRefreshLayout.finishLoadmore()
                if (t == null || t.size == 0) {
                    smartRefreshLayout.isLoadmoreFinished = true
                } else {
                    adapter.addAllItems(t)
                    smartRefreshLayout.isLoadmoreFinished = loadMore == null || loadMore.last
                }
            }
        }
    }

    override fun loadFailure(message: String?) {
        message?.let {
            mContext.toast(it)
        }

        when (status) {

            0 -> {
                smartRefreshLayout.finishRefresh()
                showEmptyView()
            }

            1 -> {
                smartRefreshLayout.finishLoadmore()
                smartRefreshLayout.isLoadmoreFinished = true
                showContentView()
            }
        }
    }

    protected open fun showEmptyView() {
        progressBar.visibility = View.GONE
        smartRefreshLayout.visibility = View.GONE
        viewStub.visibility = View.VISIBLE
    }

    protected open fun showContentView() {
        progressBar.visibility = View.GONE
        viewStub.visibility = View.GONE
        smartRefreshLayout.visibility = View.VISIBLE
    }

    protected open fun showProgressView() {
        progressBar.visibility = View.VISIBLE
        viewStub.visibility = View.GONE
        smartRefreshLayout.visibility = View.GONE
    }

    protected open fun initEmptyResource() {

    }

    protected fun refresh() {
        status = 0
        startPage = 1
        loadData()
    }


    protected fun smoothScrollToPosition(position: Int) {
        if (adapter.list.size > position && position >= 0) {
            recyclerView.smoothScrollToPosition(position)
        }
    }


    protected open fun initSwipeMenu() {

    }


    protected abstract fun createAdapter(): BaseAdapter<K>


    protected abstract fun loadData()
}